//
//  UIViewController+Addtions.m
//  HuiXianxia
//
//  Created by hzdlapple2 on 15/11/12.
//  Copyright © 2015年 何霄云. All rights reserved.
//

#import "UIViewController+KNAddtions.h"


@implementation UIViewController (KNAddtions)

- (void)KNRemoveSelfFromStackAndPushViewController:(UIViewController *)vc {
    if (!self.navigationController) {
        return;
    }
    NSMutableArray *tempArray = @[].mutableCopy;
    tempArray = self.navigationController.viewControllers.mutableCopy;
    [tempArray removeObject:self];
    [tempArray addObject:vc];
    [self.navigationController setViewControllers:tempArray animated:YES];
}

- (void)KNRemoveSelfFromStackAndPushViewControllers:(NSArray *)vcs {
    if (!self.navigationController) {
        return;
    }
    NSMutableArray *tempArray = @[].mutableCopy;
    tempArray = self.navigationController.viewControllers.mutableCopy;
    [tempArray removeObject:self];
    [tempArray addObjectsFromArray:vcs];
    [self.navigationController setViewControllers:tempArray animated:YES];
}

- (UIViewController *)KNFindViewControllerFromStack:(Class)class {
    if (!self.navigationController) {
        return nil;
    }
    NSMutableArray *tempArray = @[].mutableCopy;
    __block UIViewController *find = nil;
    tempArray = self.navigationController.viewControllers.mutableCopy;
    [tempArray enumerateObjectsUsingBlock:^(UIViewController *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:class]) {
            find = obj;
            *stop = YES;
        }
    }];
    return find;
}

- (void)KNRemoveViewControllerFromStack:(UIViewController *)removeVC {
    if (!self.navigationController) {
        return;
    }
    NSMutableArray *tempArray = @[].mutableCopy;
    tempArray = self.navigationController.viewControllers.mutableCopy;
    [tempArray removeObject:removeVC];
    [self.navigationController setViewControllers:tempArray animated:YES];
}

- (void)KNRemoveViewControllerFromStack:(UIViewController *)removeVC andPushViewController:(UIViewController *)toVc {
    if (!self.navigationController) {
        return;
    }
    NSMutableArray *tempArray = @[].mutableCopy;
    tempArray = self.navigationController.viewControllers.mutableCopy;
    [tempArray removeObject:removeVC];
    [tempArray addObject:toVc];
    [self.navigationController setViewControllers:tempArray animated:YES];
}

- (void)KNRemoveFromStack {
    if (!self.navigationController) {
        return;
    }
    NSMutableArray *tempArray = @[].mutableCopy;
    tempArray = self.navigationController.viewControllers.mutableCopy;
    [tempArray removeObject:self];
    [self.navigationController setViewControllers:tempArray animated:YES];
}

+ (UIViewController *)KNGetCurrentViewController {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    UIViewController *rootViewController = keyWindow.rootViewController;
    return [UIViewController KNGetVisibleViewControllerFrom:rootViewController];
}

+ (UIViewController *)KNGetVisibleViewControllerFrom:(UIViewController *) vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [UIViewController KNGetVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [UIViewController KNGetVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [UIViewController KNGetVisibleViewControllerFrom:vc.presentedViewController];
        } else {
            return vc;
        }
    }
}

- (UIViewController *)KNGetPresentedViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    if (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    
    return topVC;
}


@end
